package com.leostadyn.stackfarm.timestatistics.view.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.leostadyn.stackfarm.timestatistics.R;
import com.leostadyn.stackfarm.timestatistics.adapter.MainPersonnelAdapter;
import com.leostadyn.stackfarm.timestatistics.model.bean.EmployeeBean;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

public class PersonnelFragment extends Fragment implements MainPersonnelAdapter.OnRecyclerItemClickListener, OnRefreshListener {
    SmartRefreshLayout smartRefreshLayout;
    private ArrayList<EmployeeBean> arrayList;
    private MainPersonnelAdapter adapter;
    private PersonnelCallBack personnelCallBack;
    private RecyclerView recyclerView;
    TextView txt;
    public double total;

    public static PersonnelFragment newInstance(ArrayList<EmployeeBean> arrayList) {
        PersonnelFragment personnelFragment = new PersonnelFragment();
        Bundle bundle = new Bundle();
        bundle.putParcelableArrayList("personnel", arrayList);
        personnelFragment.setArguments(bundle);
        return personnelFragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_personnel, container, false);
        initView(view);
        return view;
    }

    void initView(View view) {
        Bundle bundle = getArguments();
        if (bundle != null) {
            arrayList = bundle.getParcelableArrayList("personnel");

            if (arrayList != null) {
                recyclerView = view.findViewById(R.id.mylist_personnel);
                recyclerView.setLayoutManager(new LinearLayoutManager(view.getContext()));

                smartRefreshLayout = view.findViewById(R.id.personnel_srl_area);
                smartRefreshLayout.setOnRefreshListener(this);
                smartRefreshLayout.setEnableLoadMore(false);
                txt = view.findViewById(R.id.txt_total_working_hours);
                txt.setText(personnelCallBack.setPersonnelTitle());

                if (arrayList.size() == 0) {
                    recyclerView.setVisibility(View.GONE);
                    view.findViewById(R.id.layout_no_data).setVisibility(View.VISIBLE);

                } else {
                    for (int i = 0; i < arrayList.size(); i++) {
                        total += arrayList.get(i).getWorkingHours();
                    }
                    personnelCallBack.setProjectList(arrayList);
                    adapter = new MainPersonnelAdapter(arrayList);
                    adapter.setRecyclerItemClickListener(this);
                    recyclerView.setAdapter(adapter);
                    recyclerView.setVisibility(View.VISIBLE);
                    view.findViewById(R.id.layout_no_data).setVisibility(View.GONE);
                }
                personnelCallBack.setProjectWorkingHours(total);
            }
        }

    }

    private SetPersonnelCallBack callBack;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        personnelCallBack = (PersonnelCallBack) getActivity();
        callBack = (SetPersonnelCallBack) getActivity();
    }

    @Override
    public void onItemClick(int position, List<EmployeeBean> dataBeanList) {
        callBack.mainPersonnelClick(position, dataBeanList);
    }


    public interface SetPersonnelCallBack {
        void mainPersonnelClick(int position, List<EmployeeBean> dataBeanList);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        personnelCallBack.sentPersonnelMessage();
    }

    public interface PersonnelCallBack {
        void sentPersonnelMessage();

        void setProjectList(ArrayList<EmployeeBean> arrayList);
        String setPersonnelTitle();
        void setProjectWorkingHours(double d);
    }
}
